package cj.web.marriage.controllers;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.DateUtils;
import cj.utils.ExportExcel;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.constants.XQOrderStatusEnum;
import cj.web.marriage.constants.XQOutputTypeEnum;
import cj.web.marriage.constants.XQYHStatusEnum;
import cj.web.marriage.dao.XQExportDAO;

@Path("marriageXQExport")
public class MarriageXQExportController extends BaseController{
	
	private static Logger logger = Logger.getLogger(MarriageInforController.class.getName());

	@Autowired
	private XQExportDAO xqExportDAO;
	
	@Get("show")
	public String show(Model model){
		model.add("MemuEnum",MemuEnum.xqsjdc);
        EnumSet<XQOutputTypeEnum> marriageOutputTypeEnum = EnumSet.allOf(XQOutputTypeEnum.class);
        model.add("OutputEnum",marriageOutputTypeEnum);
        return "marriageXQ/xqOutputList.vm";
	}
	
	@Get("output")  
	public void output(@Param("start")String start,@Param("end")String end, @Param("type")int type, 
			 HttpServletRequest request, HttpServletResponse response) throws Exception {  
		 String sheetName = XQOutputTypeEnum.getName(type);
		 String title = URLEncoder.encode(sheetName, "UTF-8");  // 进行转码，使其支持中文文件名  
	     response.setContentType("application/vnd.ms-excel");  // 生成提示信息 
	     response.setHeader("content-disposition", "attachment;filename=" + title + ".xls");  
	     OutputStream fOut = response.getOutputStream();
	     
	     List<Map> dataset = null;
	     ExportExcel<Map> ex = new ExportExcel<Map>();
	     
	     if(type == XQOutputTypeEnum.GGHYYHXX.getValue()){
	    	 //公共会员约会信息
	    	 String[] headers = { "序号","约方","被约方","约会时间","约会地点","约会描述","约会状态","创建时间" };
	         String[] fields = {"id","userPhone","objectUserPhone","appointDate","address","descs","appointStatus","createAt"};
	         dataset = xqExportDAO.pubYHExcel(start, end);
	         for(Map map : dataset){
	        	 map.put("appointStatus",XQYHStatusEnum.getName(Integer.valueOf(map.get("appointStatus").toString())));
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }else if(type == XQOutputTypeEnum.GGHDDDXX.getValue()){
	    	//公共活动订单信息
	    	 String[] headers = { "序号","活动名称","用户账号","订单编号","状态","创建时间" };
	         String[] fields = {"id","pubActivityName","userPhone","orderNo","status","createAt" };
	         dataset = xqExportDAO.pubActivityOrderExcel(start,end);
	         for(Map map : dataset){
	        	 map.put("status",XQOrderStatusEnum.getName(Integer.valueOf(map.get("status").toString())));
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }else if(type == XQOutputTypeEnum.SQHYYHXX.getValue()){
	    	 //社区会员约会信息
	    	 String[] headers = { "序号","约方","被约方","约会时间","约会地点","约会描述","约会状态","创建时间" };
	         String[] fields = {"id","userPhone","objectUserPhone","appointDate","address","descs","appointStatus","createAt"};
	         dataset = xqExportDAO.cmpYHExcel(start, end);
	         for(Map map : dataset){
	        	 map.put("appointStatus",XQYHStatusEnum.getName(Integer.valueOf(map.get("appointStatus").toString())));
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }else if(type == XQOutputTypeEnum.SQHDDDXX.getValue()){
	    	//社区活动订单信息
	    	 String[] headers = { "序号","活动名称","用户账号","订单编号","状态","创建时间" };
	         String[] fields = {"id","cmpActivityName","userPhone","orderNo","status","createAt" };
	         dataset = xqExportDAO.cmpActivityOrderExcel(start,end);
	         for(Map map : dataset){
	        	 map.put("status",XQOrderStatusEnum.getName(Integer.valueOf(map.get("status").toString())));
	         }
	         ex.exportExcel(sheetName, headers, fields, dataset, fOut, DateUtils.DATE_FULL_STR);
	     }
	     
		 fOut.flush();
		 fOut.close();
	 }  
}
